/*
 * Created on 2006-4-28 2:03:23
 */
package cn.com.sbell.vss.csg.vsclient.util;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.IPCameraInfo;



/**
 * @author liang.a.chen
 */
public class BeanValidator {
	
	public static void validateNotNull(PropertiesHolder holder, String propertyName) {
		String property = null;
		
		property =holder.getStrProperty(propertyName);
		
		if (Util.strIsNull(property)) {
			throw new VssException(VssConstants.CODE_REQUIRED,
					new String[] { "csg." + propertyName });
		}
	}

	public static void validateNotNull(PropertiesHolder holder, String[] propertyNames) {
		for (int i = 0; i < propertyNames.length; i++) {
			validateNotNull(holder, propertyNames[i]);
		}
	}
	

	public static void validateNotNull(Object bean, String propertyName) {
		Object property = null;
		try {
			property = PropertyUtils.getProperty(bean, propertyName);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		if (property == null) {
			throw new VssException(VssConstants.CODE_REQUIRED,
					new String[] { "csg." + propertyName });
		}
	}

	public static void validateNotNull(Object bean, String[] propertyNames) {
		for (int i = 0; i < propertyNames.length; i++) {
			validateNotNull(bean, propertyNames[i]);
		}
	}

	public static void validateProp(Object bean, String propertyName,
			int minLenght, int maxLength) {
		String property = null;
		boolean matched = false;
		try {
			property = BeanUtils.getProperty(bean, propertyName);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		if (property == null) {
			throw new VssException(VssConstants.CODE_REQUIRED,
					new String[] { "csg." + propertyName });
		}
		if (property.length() < minLenght || property.length() > maxLength) {
			throw new VssException(VssConstants.CODE_INVALID,
					new String[] { "csg." + propertyName });
		}
		for (int i = 0; i < property.length(); i++) {
			if (Character.toString(property.charAt(i)).matches(
					"(:|\"|<|>|&|'| )")) {
				matched = true;
				break;
			} else {
				matched = false;
				continue;
			}
		}
		if (matched) {
			throw new VssException(VssConstants.CODE_INVALID,
					new String[] { "csg." + propertyName });
		}
	}

	public static void validateProp(Object bean, String[] propertyNames,
			int minLength, int maxLength) {
		for (int i = 0; i < propertyNames.length; i++) {
			validateProp(bean, propertyNames[i], minLength, maxLength);
		}
	}

	public static void validatePanRange(Object bean, String propertyName,
			String productVersion) {
		Object property = null;
		Integer pro = null;
		try {
			property = PropertyUtils.getProperty(bean, propertyName);
		} catch (IllegalAccessException e) {

		} catch (InvocationTargetException e) {

		} catch (NoSuchMethodException e) {

		}
		pro = (Integer) property;

		if (productVersion.startsWith(VssConstants.MODEL_TYPE)
				|| productVersion.startsWith(VssConstants.MODEL_10TYPE)) {
			if (pro.intValue() < -5000 || pro.intValue() > 5000) {
				throw new VssException(VssConstants.CODE_INVALID,
						new String[] { "csg." + propertyName });
			}
		} else if (productVersion.startsWith(VssConstants.MODEL_381TYPE)
				|| productVersion.startsWith(VssConstants.PRODUCT_VERSION_KX)) {
			if (pro.intValue() < -17500 || pro.intValue() > 17500) {
				throw new VssException(VssConstants.CODE_INVALID,
						new String[] { "csg." + propertyName });
			}
		} else {
			if (pro.intValue() < -6000 || pro.intValue() > 6000) {
				throw new VssException(VssConstants.CODE_INVALID,
						new String[] { "csg." + propertyName });
			}
		}

	}

	public static void validatePanRange(Object bean, String[] propertyNames,
			String productVersion) {
		for (int i = 0; i < propertyNames.length; i++) {
			validatePanRange(bean, propertyNames[i], productVersion);
		}
	}

	public static void validateTiltRange(Object bean, String[] propertyNames,
			String productVersion, Integer setupType) {
		for (int i = 0; i < propertyNames.length; i++) {
			validateTiltRange(bean, propertyNames[i], productVersion, setupType);
		}
	}

	public static void validateTiltRange(Object bean, String propertyName,
			String productVersion, Integer setupType) {
		Object property = null;
		Integer pro = null;
		try {
			property = PropertyUtils.getProperty(bean, propertyName);
		} catch (IllegalAccessException e) {

		} catch (InvocationTargetException e) {

		} catch (NoSuchMethodException e) {

		}
		pro = (Integer) property;

		if (productVersion.startsWith(VssConstants.MODEL_TYPE)
				|| productVersion.startsWith(VssConstants.MODEL_10TYPE)) {
			if (pro.intValue() < -4000 || pro.intValue() > 1000) {
				throw new VssException(VssConstants.CODE_INVALID,
						new String[] { "csg." + propertyName });
			}
		} else if (productVersion.startsWith(VssConstants.MODEL_381TYPE) || productVersion.startsWith(VssConstants.PRODUCT_VERSION_KX)) {
			if (VssConstants.DATA_INTEGER == setupType.intValue()) {
				if (pro.intValue() < -12000 || pro.intValue() > 0) {
					throw new VssException(VssConstants.CODE_INVALID,
							new String[] { "csg." + propertyName });
				}
			} else {
				if (pro.intValue() < 0 || pro.intValue() > 9000) {
					throw new VssException(VssConstants.CODE_INVALID,
							new String[] { "csg." + propertyName });
				}
			}

		} else {
			if (pro.intValue() < -4500 || pro.intValue() > 2000) {
				throw new VssException(VssConstants.CODE_INVALID,
						new String[] { "csg." + propertyName });
			}
		}

	}

	public static void main(String[] args) {
		IPCameraInfo info = new IPCameraInfo();
		info.setPanRM(new Integer(-17900));
		info.setPanRP(new Integer(-17000));
		info.setPanHome(new Integer(0));
		BeanValidator.validatePanRange(info, new String[] { "panRM", "panRP",
				"panHome" }, "BB-HCM381CN");
	}
}
